home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / dsiic2.zip / L_POPUP.C < prev    next >
C/C++ Source or Header  |  1991-10-02  |  5KB  |  157 lines

  1. /* Copyright (c) James L. Pinson 1990,1991  */
  2.  
  3. /**********************   L_POPUP.C   ***************************/
  4.  
  5. #include "mydef.h"
  6. #include <string.h>
  7. #include <stdio.h>
  8. #include <conio.h>
  9. #include <ctype.h>
  10.  
  11.  
  12. /*****************************************************************
  13.  
  14.  Usage: int pop_up(struct pop_struc pop_menu[],int x,int y,
  15.                    char normal, char inverse)
  16.  
  17.  
  18.   struct pop_struc pop_menu=  data structure containing menu
  19.                               options.
  20.  
  21.   int x,y= upper left corner of pop_up window.
  22.  
  23.   char normal,inverse= text attributes used for regular and
  24.                        high-lighted options.
  25.  
  26.   Creates and manages a pop-up menu at the location specified.
  27.  
  28. *****************************************************************/
  29.  
  30. int pop_up(struct pop_struc pop_menu[],int x,int y, char normal,
  31.            char inverse)
  32. {
  33. extern struct  screen_structure scr;
  34. extern struct window_structure w[];
  35.  
  36. int col;              /* screen column */
  37. int i=0,j;            /* general purpose index variables */
  38. int width;            /* width of window */
  39. int nu_opt;           /* number of options */
  40. int cur_opt;          /* current option (highlighted */
  41. char ch,ext;          /* character and extension */
  42. int found = FALSE;    /* flag to indicate option found(selected) */
  43. int return_code;      /* return code */
  44. int pop_window;
  45. int old_caps=scr.bold_caps;  /* the original value of bold caps */
  46. /* set on bold caps to highlight menu quick keys */
  47. scr.bold_caps=ON;  
  48.  
  49. /* figure how many options there are */
  50.  
  51. nu_opt=0;
  52.  
  53. /* loop until empty string found */
  54. while (pop_menu[i++].name[0]!='\0'); 
  55.  
  56. nu_opt=i-1;   /* set nu_opt to the number of options found */
  57.  
  58. /* figure size of box */
  59.  
  60.   width=0;      /* figure max length of window */
  61.  
  62.   for (i=0;i< nu_opt;i++){                   /* for each option */
  63.    /* find largest option length */
  64.    if (strlen(pop_menu[i].name) > width){
  65.     width= strlen(pop_menu[i].name);   
  66.    }
  67.   }
  68.  
  69. /* make a window based on x,y parameters
  70.    and calculated width and height */
  71.  
  72. pop_window= win_make(x,y,width,nu_opt,STD_FRAME,"",normal,normal);
  73.  
  74. cursor(NO_CURSOR);    /* hide cursor */
  75.  
  76. scr.current = scr.normal;
  77.  
  78. cur_opt = 0;   /* first option */
  79.  
  80.   /* infinite loop */
  81.    for(;;){
  82.  
  83.    scr.bold_caps=!found; /* turn off scr.bold_caps if true */
  84.  
  85.        /* print menu options, highlight current option */
  86.          col=1;                     /* start at first column */
  87.          for(i=0;i< nu_opt;i++){    /* print each option */
  88.            /* highlight current option */
  89.            if(i == cur_opt) scr.current= inverse;  
  90.             else scr.current=normal;          /* else normal */
  91.             print(1,col++,pop_menu[i].name);
  92.          };
  93.  
  94.           if(found ) {          /* selection found */
  95.  
  96.             /* return specified code if NULL */
  97.         if(pop_menu[cur_opt].fun==NULL){
  98.              return_code=pop_menu[cur_opt].select_id;
  99.              break;
  100.             }
  101.             else
  102.               /* a function was specified by pointer,
  103.                  call the function and get code */
  104.  
  105.              return_code=(*pop_menu[cur_opt].fun)() ;
  106.  
  107.              win_pop_top(pop_window);
  108.              /* a non-zero value is signal to exit */
  109.              if(return_code!=0) break;  
  110.              found = FALSE;
  111.              /* make sure keyboard buffer is clear */
  112.              if (kbhit()) getch();  
  113.  
  114.           } /* end if(found) */
  115.  
  116.           else{  /* not found */
  117.            /* read keys until selection is made */
  118.            get_key(&ch,&ext); /* get a character */
  119.            ch=toupper(ch);    /* make it upper case */
  120.           }
  121.            if (ext == DOWN)  cur_opt++;  /* move down */
  122.            if (ext == UP)    cur_opt--;  /* move up */
  123.            /* wrap if boundaries exceeded */
  124.            if (cur_opt >= nu_opt) cur_opt =0;
  125.            if (cur_opt < 0) cur_opt = nu_opt-1;
  126.  
  127.           if (ch== 13) found = TRUE;
  128.  
  129.           if(ch!='\0'){  /* do we have a letter? */
  130.             for(i=0;i<nu_opt;i++){   /* scan each option? */
  131.              j=0;
  132.  
  133.              /* check each letter within option */
  134.              while(pop_menu[i].name[j]!=0){  
  135.                /* ignore spaces in string */
  136.                if (pop_menu[i].name[j++]==ch && ch != ' '){
  137.                 cur_opt = i;
  138.                 found = TRUE;
  139.                }
  140.              } /* end while */
  141.             }
  142.            } /* end ch!='\0' */
  143.  
  144.  
  145.            if (ch==ESCAPE){  /* EXIT IF ESCAPE KEY */
  146.             return_code=0;
  147.             break;
  148.            }
  149.            ext=' ';ch=' ';
  150.    } /* end for(;;)*/
  151.  
  152.    scr.bold_caps=old_caps;   /* restore bold caps */
  153.    win_delete_top();         /* remove top window */
  154.  
  155.    return (return_code);
  156. }
  157.